﻿#region File Description
//-----------------------------------------------------------------------------
// Settings.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// Modification author: Hristo Hristov
// Modification date  : 29.04.2011 
// Modified Version   : 1.0
// Description        : Modified version for NOVA as an abstract base class, 
//                      which can be used to derive different settings classes 
//                      by only adding variables for the setting parameters in
//                      the derived classes. The "Load" and Save" functions in 
//                      this base class reads and writes all public members of 
//                      the derived class structure in XML type into the file. 
//
// Modifications      : ### 29.04.2011 ###
//                      - The private members of the derived class will NOT be
//                        written into the XML file!
//                      - The only member "typeDerivate" of this class must be 
//                        set to the type of the derived class using the "typeof"
//                        operator in it's contructor!
//                      - When using the "Load" function, convert the returning
//                        object into the derived class type using type cast
//                        methods like "(YourClass) mySettings.Load(...);" or
//                        "mySettings.Load(...) as YourClass;"
//
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.IO;
using System.Xml.Serialization;
#endregion

namespace NOVA
{
    /// <summary>
    /// The Setting class handles loading and saving of global application settings.
    /// The normal .Net classes (System.Configuration) for doing this are not available on the CF (and therefore 360)
    /// </summary>
    public class Settings
    {
        /// <summary>
        /// This variable represents the type of class, which structure will
        /// be analysed in the "Load" and "Save". Set this to the type of the
        /// derived class using the "typeof" operator in the constructor of
        /// the derived class!
        /// </summary>
        public static System.Type typeDerivative = typeof(Settings);

        

        /// <summary>
        /// Saves the current settings. This cannot be static and must be only 
        /// used through the instance!!
        /// </summary>
        /// <param name="filename">The filename to save to</param>
        public void Save(string filename)
        {
            Stream stream = File.Create(filename);

            XmlSerializer xmlSerializer = new XmlSerializer(typeDerivative);
            xmlSerializer.Serialize(stream, this); 
            stream.Close();
        }

        /// <summary>
        /// Loads settings from a file
        /// </summary>
        /// <param name="filename">The filename to load</param>
        public static object Load(string filename)
        {
            Stream stream = File.OpenRead(filename);
            XmlSerializer xmlSerializer = new XmlSerializer(typeDerivative);
            return xmlSerializer.Deserialize(stream);
        }
    }
}
